home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / nelson / demo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  4.3 KB  |  139 lines

  1. /* ----------------------------------------------------
  2.  *  Listing 9
  3.  *
  4.  *  demo.cpp
  5.  *  background print utility for testing
  6.  *  IOCTL handle functions
  7.  * ------------------------------------------------- */
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <alloc.h>
  11. #include <io.h>
  12. #include <errno.h>
  13. #include "chandle.h"
  14. #include "fhandle.h"
  15.  
  16. #define DEFAULT_SLICE   100
  17. #define DEFAULT_BUFSIZE 512
  18. #define ENABLE  1
  19. #define DISABLE 0
  20. #define TRUE    0
  21. #define FALSE  -1
  22. #define  min(x,y)    (((x) < (y)) ? (x) : (y))
  23.  
  24. class FilePrint  {
  25. public:
  26.     FilePrint() {
  27.         _slice_cnt = DEFAULT_SLICE;
  28.         _hold = _master_switch = DISABLE;
  29.         _inf = (fileHandle *) 0;
  30.         _dev = (charHandle *) 0;
  31.         _outbuf = _bufptr = (char *) 0;
  32.         _bufcnt = 0;
  33.         }
  34.     ~FilePrint() {
  35.         if( _dev )  delete _dev;
  36.         if( _inf )  delete _inf;
  37.         if( _outbuf ) free( (void *) _outbuf );
  38.         }
  39.     int Status( void) { return _hold; }
  40.     void Status( int flag ) { _hold = flag; }
  41.     void resetCount( int cnt ) { _slice_cnt = cnt; }
  42.     int attachDevice( const char *device );
  43.     int submitFile( const char *path );
  44.     int backgroundOutput(void);
  45. protected:
  46.     int _slice_cnt, _hold, _master_switch, _bufcnt;
  47.     char *_outbuf, *_bufptr;   //-> output buffer
  48.     charHandle *_dev;   //-> output device
  49.     fileHandle *_inf;   //-> file to print
  50.     virtual int output(int);
  51.     virtual int input(void);
  52. };  // .... end class FilePrint
  53.  
  54. int FilePrint::attachDevice( const char *device )
  55. {
  56.    /* Initialize a new output device and allocate an
  57.     * output buffer.  Return master status flag ....
  58.     */
  59.     if( _dev )  delete _dev;
  60.     _dev = charHandle::Init( device );
  61.     if( !_outbuf )
  62.         _outbuf = (char *) malloc( DEFAULT_BUFSIZE );
  63.     _master_switch = ( !_dev || !_outbuf ) ?
  64.                                    DISABLE : ENABLE;
  65.     return _master_switch;
  66. }
  67. int FilePrint::submitFile( const char *path )
  68. {   //submit a file to print, return print status...
  69.     if( _hold == ENABLE || _inf ||
  70.                        _master_switch == DISABLE )
  71.         return FALSE;   //already printing or disabled
  72.     _inf = fileHandle::Init( path );  //open the file
  73.     _hold = _inf ? ENABLE : DISABLE;  //start up if OK
  74.     return _hold;
  75. }
  76. int FilePrint::backgroundOutput(void)
  77. {   //Print up to _slice_cnt bytes,
  78.     //return #bytes output .......
  79.     if( _hold == DISABLE ||
  80.           _master_switch == DISABLE )  return 0;
  81.     if( _bufcnt == 0 )  {  //refill the buffer
  82.         if( (_bufcnt = input()) == 0 )  {   //at EOF
  83.             _hold = DISABLE;
  84.             delete _inf;    //close down and
  85.             _inf = NULL; //indicate job finished
  86.             return 0;
  87.         }
  88.         _bufptr = _outbuf;
  89.     }
  90.     int cnt, num_out = 0;
  91.     cnt = min(_slice_cnt,_bufcnt);
  92.     for( ; cnt; cnt-- )     {
  93.         if( output( (int) *_bufptr ) == TRUE )  {
  94.             _bufptr++;
  95.             num_out++;
  96.             _bufcnt--;
  97.         }
  98.     }   return num_out;
  99. }
  100. int FilePrint::output(int ch)   //virtual
  101. {   //output a byte ........
  102.     if( !_dev->deviceReadyOut() )
  103.         return FALSE;
  104.     int rval = write( _dev->readHandle(),
  105.                                (void *) &ch, 1 );
  106.     return rval > 0 ? TRUE : FALSE;
  107. }
  108. int FilePrint::input(void)          //virtual
  109. {   //refill output buffer, return #bytes read .....
  110.     return read( _inf->readHandle(), (void *) _outbuf,
  111.                                   DEFAULT_BUFSIZE );
  112. }
  113. int main( int argc, char **argv )   {
  114.     char device[] = "LPT1";
  115.     FilePrint fp;
  116.     if( argc < 2 )  {
  117.         printf("Need at least 1 file to print...\n");
  118.         return 1;
  119.     }
  120.     else if( fp.attachDevice( device ) == DISABLE ) {
  121.         printf("Unable to attach %s for output\n",
  122.                                             device );
  123.         return 1;
  124.     }
  125.     for( int i = 1; argv[i]; i++ )      {
  126.         if( fp.submitFile(argv[i]) == DISABLE )     {
  127.             printf("Unable to open %s\n", argv[i] );
  128.             continue;
  129.         }
  130.         while( fp.Status() == ENABLE )    {
  131.         //echo keys pressed to screen while printing
  132.             if( kbhit() )
  133.                 if( getche() == 27 ) return 0; //quit
  134.             fp.backgroundOutput();
  135.         }
  136.     }   return 0;
  137. }
  138. /* ----- End of file ------------------------------- */
  139.